package com.boot.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.boot.framework.aop.Log;
import com.boot.framework.common.R;
import com.boot.system.dto.SysUserDTO;
import com.boot.system.service.ISysUserService;
import com.boot.system.wrapper.SysUserWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * @author yangfan
 * @date 2024/1/7
 * @Description
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/user")
@Tag(name = "用户管理")
public class SysUserController {
    private ISysUserService sysUserService;

    @GetMapping("page")
    @SaCheckPermission(value = "system.user.page")
    @Operation(summary = "分页")
    public R page(@RequestParam Map<String, Object> params) {

        return R.ok(sysUserService.selectPage(params));
    }

    @GetMapping("list")
    @SaCheckPermission(value = "system.user.list")
    @Operation(summary = "集合")
    public R list(Map<String, Object> params) {
        return R.ok(sysUserService.selectList(params));
    }


    @PostMapping
    @Log(title = "用户管理", content = "新增")
    @SaCheckPermission(value = "system.user.add")
    @Operation(summary = "新增")
    public R insert(@RequestBody SysUserDTO sysUserDTO) {
        return R.ok(sysUserService.save(SysUserWrapper.build().dtoEntity(sysUserDTO)));
    }


    @PutMapping
    @Log(title = "用户管理", content = "修改")
    @SaCheckPermission(value = "system.user.update")
    @Operation(summary = "修改")
    public R update(@RequestBody SysUserDTO sysUserDTO) {
        return R.ok(sysUserService.updateById(SysUserWrapper.build().dtoEntity(sysUserDTO)));
    }

    @GetMapping
    @SaCheckPermission(value = "system.user.info")
    @Operation(summary = "查看")
    public R view(@RequestParam("id") String id) {
        return R.ok(sysUserService.getById(id));
    }

    @DeleteMapping
    @SaCheckPermission(value = "system.user.delete")
    @Log(title = "用户管理", content = "删除")
    @Operation(summary = "删除")
    public R delete(String ids) {
        return R.ok(sysUserService.removeBatchByIds(Arrays.asList(StringUtils.split(ids, ","))));
    }

    /**
     * 根据账号查询用户
     *
     * @param username
     * @return
     */
    @GetMapping("/username/{username}")
    @Operation(summary = "查询")
    R<SysUserDTO> selectByUsername(@PathVariable("username") String username) {
        return R.ok(sysUserService.selectByUsername(username));
    }
}
